package com.huatai.manage.service.impl;

import com.huatai.common.config.MinioAPI;
import com.huatai.manage.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import static com.huatai.manage.common.constant.ComConstant.MODEL_BUCKET;

/**
 * @ClassName MinioServiceImpl
 * @Description TODO
 * @Author luoxc
 * @Date 2023/3/14 13:24
 * @Version 1.0
 */
@Service
public class MinioServiceImpl implements MinioService {

	@Autowired
	private MinioAPI minioAPI;

	@Override
	public void makeBucket(String bucket){
		try {
			minioAPI.makeBucket(bucket);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	@Override
	public void setBucketPolicy(String bucket, String policy){
		//"read-only" "write-only"  "read-write"  "none"
		try {
			minioAPI.setBucketPolicy(bucket, policy);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	@Override
	public String uploadFile(MultipartFile file){
		try {
			return minioAPI.uploadFile(MODEL_BUCKET, file.getOriginalFilename(), "");
		} catch (Exception exception) {
			exception.printStackTrace();
		}

		return null;
	}

	@Override
	public String uploadInputStream(MultipartFile file){
		try {
			return minioAPI.uploadInputStream(MODEL_BUCKET, file.getOriginalFilename(), file.getInputStream(), file.getContentType());
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return null;
	}

	@Override
	public InputStream download(String objectKey){
		try {
			return minioAPI.download(MODEL_BUCKET, objectKey);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return null;
	}

	@Override
	public String getSignedUrl(String objectKey, int expires){
		try {
			minioAPI.getSignedUrl(MODEL_BUCKET, objectKey, expires);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return null;
	}

	@Override
	public void deleteFile(String objectKey){
		try {
			minioAPI.deleteFile(MODEL_BUCKET,objectKey);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	@Override
	public List<Object> listObjects(String bucket) {
		return null;
	}
}
